package com.jztd.platfrom.base.util;

import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


public class UserUtil {
    public static String getUserId(){
        return getRequestParam("userId");
    }
    public static String getTenant(){
        return getRequestParam("tenant");
    }
    /**
     * 从当前请求头中获取指定键对应的参数值
     * @param key 请求头中的键
     * @return 对应的参数值，若不存在则返回null
     */
    public static String getRequestParam(String key) {
        // 检查key是否为空
        if (ObjectUtil.isEmpty(key)) {
            return null;
        }

        // 获取当前请求上下文
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        // 检查上下文是否存在
        if (requestAttributes == null) {
            return null;
        }

        // 获取HttpServletRequest对象
        HttpServletRequest request = requestAttributes.getRequest();

        // 从请求头中获取对应的值
        return request.getHeader(key);
    }
}
